<template>
  <div class="table-container">
    <el-table
      :data="tableData"
      border
      style="width: 100%"
      :span-method="arraySpanMethod"
    >
      <el-table-column label="序号" type="index" width="50" align="center" />
      <el-table-column
        label="计划名称"
        align="center"
        prop="createTime"
        min-width="80"
      >
        <template slot-scope="scope">
          {{ scope.row.extra1 }}
        </template>
      </el-table-column>
      <el-table-column
        label="总采购量 (吨)"
        align="center"
        prop="purchaseQuantity"
        min-width="80"
      >
        <template slot-scope="scope">
          {{ scope.row.purchaseQuantity || 0 }}
        </template>
      </el-table-column>
      <el-table-column label="炼厂分配 (吨)" align="center" min-width="80">
        <template slot-scope="scope">
          <span>
            {{ scope.row.detailInfo.refineryName }} :
            {{ scope.row.detailInfo.quantity || "--" }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="在途信息" align="center" min-width="200">
        <template slot-scope="scope">
          <span>
            {{
              scope.row.onways
                ? `${
                    scope.row.onways.startTime
                      ? "第" +
                        scope.row.onways.index +
                        "批次：" +
                        dateChange(
                          scope.row.onways.startTime,
                          "yyyy年MM月dd日"
                        ) +
                        "发油" +
                        scope.row.onways.sendCarCount +
                        "车," +
                        scope.row.onways.sendWeight +
                        "吨；预计" +
                        dateChange(
                          scope.row.onways.arriveTime,
                          "yyyy年MM月dd日"
                        ) +
                        "时间到达"
                      : ""
                  }`
                : "--"
            }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="物料状态"
        align="center"
        prop="status"
        min-width="80"
      >
        <template slot-scope="scope">
          {{ scope.row.onways ? statusFormat(scope.row.onways.extra1) : "--" }}
        </template>
      </el-table-column>
      <el-table-column label="本月已发 (吨)" align="center" min-width="80">
        <template slot-scope="scope">
          {{ scope.row.totalSendWeight }}
        </template>
      </el-table-column>
      <el-table-column label="本月剩余 (吨)" align="center" min-width="80">
        <template slot-scope="scope">
          {{ Number((scope.row.detailInfo.quantity - scope.row.totalSendWeight).toFixed(3)) }}
        </template>
      </el-table-column>
      <el-table-column label="异常情况" align="center" min-width="80">
        <template slot-scope="scope">
          {{ scope.row.onways ? scope.row.onways.error : "" }}
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  name: "planTable",
  props: {
    tableData: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      filteredStatus: [
        { value: 1, label: "未到站;未收油" },
        { value: 2, label: "已到站;未收油" },
        { value: 3, label: "已到站;已收油" },
      ],
    };
  },
  methods: {
    statusFormat(extra1, column) {
      const user = this.filteredStatus.find(
        (user) => user.value + "" === extra1 + ""
      );
      return user ? user.label : "";
    },
    dateChange(data, format) {
      // 根据format 转换年月 或者年月日
      const date = new Date(data);
      if (format === "yyyy年MM月") {
        return `${date.getFullYear()}年${date.getMonth() + 1}月`;
      } else if (format === "yyyy年MM月dd日") {
        return `${date.getFullYear()}年${
          date.getMonth() + 1
        }月${date.getDate()}日`;
      }
    },
    arraySpanMethod({ row, column, rowIndex, columnIndex }) {
      // 根据 row.extra1 相同的行进行合并 前5列和操作列
      if (
        columnIndex === 0 ||
        columnIndex === 1 ||
        columnIndex === 2
      ) {
        if (
          rowIndex > 0 &&
          row.extra1 === this.tableData[rowIndex - 1].extra1
        ) {
          return [0, 0];
        }
        const count = this.tableData.filter(
          (item) => item.extra1 === row.extra1
        ).length;
        return [count, 1];
      }

      // 在row.extra1 相同的基础上 refineryId 相同的行进行合并
      if (columnIndex === 3 || columnIndex === 6 || columnIndex === 7) {
        if (
          rowIndex > 0 &&
          row.extra1 === this.tableData[rowIndex - 1].extra1 &&
          row.detailInfo.refineryId === this.tableData[rowIndex - 1].detailInfo.refineryId
        ) {
          return [0, 0];
        }
        const count = this.tableData.filter(
          (item) =>
            item.detailInfo.refineryId === row.detailInfo.refineryId && item.extra1 === row.extra1
        ).length;
        return [count, 1];
      }
      // 其他情况不合并
      return [1, 1];
    },
  },
};
</script>

<style lang="scss" scoped>
@import url("./table.scss");
</style>
